//
//  HCFoodDetailView.m
//  ibeaconDemo
//
//  Created by liyun on 14-7-10.
//  Copyright (c) 2014年 qkong. All rights reserved.
//

#import "HCFoodDetailView.h"
#import "HCMacroDefinition.h"

static int height = 100;

@interface HCFoodDetailView ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation HCFoodDetailView
@synthesize foodDisplayArr;
@synthesize pushTimeStr;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        if(!foodDisplayArr)
            foodDisplayArr = [[NSArray alloc] init];
        
    }
    return self;
}

- (void)navigationShowLeft
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = foodDisplayArr[0][NSLocalizedString(@"HCfoods_canteen", @"")];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame     = CGRectMake(0, 0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"HCback.png"] forState:UIControlStateNormal];
    [button addTarget:self
               action:@selector(navigationShowLeft)
     forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItemleft          = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = barItemleft;
    
    UITableView *foodTable        = [[UITableView alloc] initWithFrame:self.view.frame
                                                                 style:UITableViewStyleGrouped];
    foodTable.delegate            = self;
    foodTable.dataSource          = self;
    foodTable.sectionFooterHeight = 0;
    foodTable.backgroundColor     = [UIColor whiteColor];
    foodTable.separatorStyle      = UITableViewCellSeparatorStyleNone;
    [self.view                    addSubview:foodTable];
    
    
    UISwipeGestureRecognizer *backGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                                  action:@selector(navigationShowLeft)];
    backGes.direction                 = UISwipeGestureRecognizerDirectionRight;
    [self.view                        addGestureRecognizer:backGes];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return foodDisplayArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [foodDisplayArr[section][@"foods"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.backgroundColor = [UIColor clearColor];
    }
    
    NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
    for (UIView *subview in subviews)
    {
        [subview removeFromSuperview];
    }
    
    UIImageView *line    = [[UIImageView alloc] init];
    line.backgroundColor = TABLE_LINE;
    [cell.contentView    addSubview:line];
    
    //食堂
    line.frame = CGRectMake(0, height-1, 320, 1);
    
//    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(6, 10, 60, 60)];
//    image.image = [UIImage imageNamed:@"HC1.png"];
//    [cell.contentView addSubview:image];
    
//    NSString *keyStr = [displayArr[indexPath.row] allKeys][0];
//    
//    UILabel *canteen = [[UILabel alloc] initWithFrame:CGRectMake(20, (height1-50)/2-2, 40, 50)];
//    canteen.text = displayArr[indexPath.row][keyStr][0][NSLocalizedString(@"canteen", @"")];
//    canteen.font = HEIGHT_FONT(15.0f);
//    canteen.numberOfLines = 2;
//    canteen.textColor = [UIColor whiteColor];
//    canteen.transform = CGAffineTransformMakeRotation(-M_PI/7);
//    [cell.contentView addSubview:canteen];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 270, 20)];
    name.text     = foodDisplayArr[indexPath.section][@"foods"][indexPath.row][NSLocalizedString(@"HCfoods_name", @"")];
    name.font     = HEIGHT_FONT(17.0f);
    [cell.contentView addSubview:name];
    
    UILabel *price     = [[UILabel alloc] initWithFrame:CGRectMake(275, 5, 40, 20)];
    NSString *priceStr =[NSString stringWithFormat:@"¥ %d",[foodDisplayArr[indexPath.section][@"foods"][indexPath.row][NSLocalizedString(@"HCfoods_price", @"")] intValue]];
    price.text         = priceStr;
    price.textColor    = PRICE_COLOR;
    price.font         = HEIGHT_FONT(15.0f);
    [cell.contentView  addSubview:price];
    
    UILabel *description      = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, 300, 30)];
    description.numberOfLines = 2;
    description.font          = NORMAL_FONT(12.0f);
    description.textColor     = [UIColor grayColor];
    NSString *descriptionStr  = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"HCfood_feature", @""),foodDisplayArr[indexPath.section][@"foods"][indexPath.row][NSLocalizedString(@"HCfoods_description", @"")]];
    description.text          = descriptionStr;
    [cell.contentView         addSubview:description];
    
    UILabel *ingredients      = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, 300, 30)];
    ingredients.numberOfLines = 2;
    ingredients.font          = NORMAL_FONT(12.0f);
    ingredients.textColor     = [UIColor grayColor];
    NSString *ingredientsStr  = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"HCfood_ingredient", @""),foodDisplayArr[indexPath.section][@"foods"][indexPath.row][NSLocalizedString(@"HCfoods_ingredients", @"")]];
    ingredients.text          = ingredientsStr;
    [cell.contentView         addSubview:ingredients];
    
    UILabel *pushTimeLabel      = [[UILabel alloc] initWithFrame:CGRectMake(160, height-15, 150, 15)];
    pushTimeLabel.textAlignment = NSTextAlignmentRight;
    pushTimeLabel.text          = pushTimeStr;
    pushTimeLabel.font          = NORMAL_FONT(12.0f);
    pushTimeLabel.textColor     = CONTENT_COLOR;
    [cell.contentView           addSubview:pushTimeLabel];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *customHead         = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    customHead.backgroundColor = BACK_COLOR;
    
    UILabel *pushTimeLabel     = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 310, 30)];
    pushTimeLabel.text         = foodDisplayArr[section][@"name"];
    pushTimeLabel.font         = HEIGHT_FONT(14.0f);
    pushTimeLabel.textColor    = BACK_TITLE_COLOR;
    [customHead                addSubview:pushTimeLabel];
    
    return customHead;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
